package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: zm
 * @Date: 2021/8/9 20:49
 */
@RestController // @ResponseBody + @Controller
@RequestMapping("admin/product")
public class SkuManageController {

    @Autowired
    private ManageService manageService;

    // http://api.gmall.com/admin/product/spuImageList/{spuId}
    // 根据spuId 查询spuImageList
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        List<SpuImage> list = manageService.getSpuImageList(spuId);
        return Result.ok(list);
    }

    // http://api.gmall.com/admin/product/spuSaleAttrList/{spuId}
    // 根据spuId获取销售属性
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> list = manageService.getSpuSaleAttrList(spuId);
        return Result.ok(list);
    }

    // http://api.gmall.com/admin/product/saveSkuInfo
    // 添加sku
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    // http://api.gmall.com/admin/product/list/{page}/{limit}
    // 获取sku分页列表
    @GetMapping("list/{page}/{limit}")
    public Result getSkuInfoList(@PathVariable Long page ,@PathVariable Long limit){
        Page<SkuInfo> skuInfoPage = new Page<SkuInfo>(page, limit);
        IPage skuIpage = manageService.getSkuInfoList(skuInfoPage);
        return Result.ok(skuIpage);
    }

    // http://api.gmall.com/admin/product/onSale/{skuId}
    // 上架
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }

    // http://api.gmall.com/admin/product/cancelSale/{skuId}
    // 下架
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }

}
